<?php 

	EbView::includeEbuildyCss('eb_buttons','eb_datagrid');

	$name 	= EbController::$request->categorie;
	$id		= EbController::$request->id;

	$categorie_mode 	= $name === 'categorie';
	$boutique_mode 		= $name === 'boutique';
	$reportage_mode 	= $name === 'reportage';
	
	$columns = array();
	
	$edit_mode = !is_null($id);
	
	if ($categorie_mode)
	{
		if ($edit_mode)
		{
			$fields = array();
			$action = '/VarPeople/EditCategorie.do';
			
			$fields []= (object) array('label' => 'Name','name' => 'name');
			
			if ($id != 0)
			{
				$categorie = EbModel::getModel('VarPeople','Categorie')->selectById($id);
				
				$fields[0]->value = $categorie->name;
				
				$fields []= (object) array('itemRenderer' => '<input type="hidden" name="id" value="'.$id.'" />');
			}
		}
		else
		{
			$items = EbModel::getModel('VarPeople','Categorie')->selectPrepared();
			$columns []= (object) array('headerText' => 'ID','labelField' => 'ca_id');
			$columns []= (object) array('headerText' => 'Name','itemRenderer' => '<a href="$url">$name</a>');
			$columns []= (object) array('headerText' => 'Magasins','labelField' => 'boutiques');
			$columns []= (object) array('headerText' => 'Actions','itemRenderer' => '<a class="minibutton" href="/admin-categorie?id=$ca_id"><span class="edit">Editer</span></a><a class="minibutton" href="/VarPeople/RemoveObject.do?type=2&id=$ca_id"><span class="delete">Supprimer</span></a>');
		}
	}
	elseif ($boutique_mode)
	{
		if ($edit_mode)
		{
			$fields = array();
			$action = '/VarPeople/EditBoutique.do';
			$categories = EbModel::getModel('VarPeople','Categorie')->select();
			$buffer = '';
			
			if ($id != 0)
			{
				$boutique = EbModel::getModel('VarPeople','Boutique')->selectById_1($id);
			}			
					

			
			$fields []= (object) array('label' => 'Name','name' => 'name', 'required' => true);
			$fields []= (object) array('label' => 'Catégorie','itemRenderer' => EbHTMLToolkit::newSelectTag($categories,'ca_id',isset($boutique) ? $boutique->ca_id : null,'ca_id','name'));
			$fields []= (object) array('label' => 'Description','name' => 'description','required' => true);
			$fields []= (object) array('label' => 'Activité','name' => 'activity');
			$fields []= (object) array('label' => 'Adresse','name' => 'adress');
			$fields []= (object) array('label' => 'Ville','name' => 'city');
			$fields []= (object) array('label' => 'Contact','name' => 'contact');
			
			if (isset($boutique))
			{
				$fields[0]->value = $boutique->name;
				$fields[1]->value = $boutique->ca_id;
				$fields[2]->value = $boutique->description;
				$fields[3]->value = $boutique->activity;
				$fields[4]->value = $boutique->adress;
				$fields[5]->value = $boutique->city;
				$fields[6]->value = $boutique->contact;
				
				$fields []= (object) array('label' => 'Contact','itemRenderer' => '<input type="hidden" name="bo_id" value="'.$id.'" />');
			}
		}
		else
		{
			$items = EbModel::getModel('VarPeople','Boutique')->selectListing();
			$columns []= (object) array('headerText' => 'ID','labelField' => 'bo_id');
			$columns []= (object) array('headerText' => 'Name','itemRenderer' => '<a href="/$urlname">$name</a>');
			$columns []= (object) array('headerText' => 'Catégorie','labelField' => 'categorie');
			$columns []= (object) array('headerText' => 'Activité','labelField' => 'activity');
			$columns []= (object) array('headerText' => 'Ville','labelField' => 'city');
			$columns []= (object) array('headerText' => 'Visites','labelField' => 'hits');
			$columns []= (object) array('headerText' => 'Contact','labelField' => 'contact');
			$columns []= (object) array('headerText' => 'Actions','itemRenderer' => '<a class="minibutton" href="/admin-boutique?id=$bo_id"><span class="edit">Editer</span></a><a class="minibutton" href="/VarPeople/RemoveObject.do?type=1&id=$bo_id"><span class="delete">Supprimer</span></a>');
		}
	}
	elseif ($reportage_mode)
	{
		if ($edit_mode)
		{
			$fields = array();
			$action = '/VarPeople/EditReportage.do';
			
			$fields []= (object) array('label' => 'Nom','name' => 'nom', 'required' => true);
			$fields []= (object) array('label' => 'Description','name' => 'description','required' => true);
			$fields []= (object) array('label' => 'Ville','name' => 'ville');
			$fields []= (object) array('label' => 'Date','name' => 'date');
			
			if ($id != 0)
			{
				$reportage = EbModel::getModel('VarPeople','Reportage')->selectById($id);
				
				if (!is_null($reportage))
				{
					$fields[0]->value = $reportage->nom;
					$fields[1]->value = $reportage->description;
					$fields[2]->value = $reportage->ville;
					$fields[3]->value = $reportage->date;
				}
				
				$fields []= (object) array('itemRenderer' => '<input type="hidden" name="rep_id" value="'.$id.'" />');
			}
		}
		else
		{
			$items = EbModel::getModel('VarPeople','Reportage')->select();
			$columns []= (object) array('headerText' => 'Name','labelField' => 'nom');
			$columns []= (object) array('headerText' => 'Description','labelField' => 'description');
			$columns []= (object) array('headerText' => 'Ville','labelField' => 'ville');
			$columns []= (object) array('headerText' => 'Date','labelField' => 'date');
			$columns []= (object) array('headerText' => 'Actions','itemRenderer' => '<a class="minibutton" href="/admin-reportage?id=$rep_id"><span class="edit">Editer</span></a><a class="minibutton" href="/VarPeople/RemoveObject.do?type=3&id=$rep_id"><span class="delete">Supprimer</span></a>');
		}
	}
	else
	{
		throw new Exception('This Admin categorie is not defined!');
	}
?>

<div id="content">
	<h2>Administration &gt; <?php echo $name; ?></h2>
	<br />
	<?php 
		if (!$edit_mode)
		{
			echo '<br />';
			eb_widget('datagrid',array('dataProvider' => $items, 'columns' => $columns));
			echo '<br /><p style="text-align:center;"><a class="eb-button" href="/admin-'.$name.'?id=0"><span class="add">Ajouter</span></a></p>';
		}
		else
		{
			theme_begin_box('Informations Générales');
			
			eb_widget('form',array('method' => 'POST','action' => $action,'fields' => $fields));
			
			theme_end_box();

			if ($id > 0)
			{
				if ($boutique_mode)
				{
					theme_begin_box('Carte:');
					
					eb_widget('component',array('module' => 'GoogleMaps','file' => 'AdminPlacePoint','adress' => $boutique->adress,'lon' => $boutique->lon,'lat' => $boutique->lat,'action' => '/varpeople/setboutiquemaps.do','fields' => array((object)array('name' => 'bo_id','value' => $id))));
					
					theme_end_box();
					
					theme_begin_box('Photos:');
					
					eb_widget('component',array('folder' => $boutique->mediafolder_id,'module' => 'Media','file' => 'Manager'));	
					
					theme_end_box();
				}
				elseif ($reportage_mode)
				{
					theme_begin_box('Photos:');
					
					eb_widget('component',array('folder' => $reportage->mediafolder_id,'module' => 'Media','file' => 'Manager'));	
					
					theme_end_box();
				}
			}
		}
		?>
</div>

<div id="sidebar">
	<div class="vp-menu">
		<ul>
			<li><a class="" href='/admin-categorie'><span class="list">Catégories</span></a></li>
			<li><a class="" href='/admin-boutique'><span class="list">Boutiques</span></a></li>
			<li><a class="" href='/admin-reportage'><span class="list">Reportages</span></a></li>
		</ul>
	</div>
</div>